package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.UserMapper;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户操作的控制器后端接口
 * @RestController设置为所有请求映射方法返回值作为响应正文
 */
@RestController
@RequestMapping("/user")
public class UserController {

    // 注入用户mybatis的mapper接口
    @Autowired
    private UserMapper userMapper;

    // 注入密码加密对象
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 测试登录接口
     * @param username 账号
     * @param password 密码
     * @param request 注入的HttpServletRequest对象
     * @return
     */
    @RequestMapping("/login1")
    public ResponseBodyMessage<User> login1 (@RequestParam String username, @RequestParam String password,
                                           HttpServletRequest request) {

        // 创建用户对象，用户数据库查询
        User userLogin = new User();
        // 设置账号
        userLogin.setUsername(username);
        // 设置密码
        userLogin.setPassword(password);

        // 验证账号密码
        User user = userMapper.login(userLogin);

        // 查询不到，说明账号密码错误
        if(user == null) {
            System.out.println("登录失败！");
            return new ResponseBodyMessage<>(-1,"登录失败！",userLogin);
        }else {
            // 查询到用户，验证账号密码成功，创建session并保存用户信息
            //request.getSession().setAttribute("USERINFO_SESSION_KEY",user);
            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
            return new ResponseBodyMessage<>(0,"登录成功！",userLogin);
        }
    }


    /**
     * 用户登录接口
     * @param username 账号
     * @param password 密码
     * @param request 注入的HttpServletRequest对象
     * @return
     */
    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestParam String username, @RequestParam String password,
                                            HttpServletRequest request) {

        // 根据账号查询用户
        User user = userMapper.selectByName(username);

        // 查询不到，说明账号错误
        if(user == null) {
            System.out.println("登录失败！");
            return new ResponseBodyMessage<>(-1,"用户名或者密码错误！",user);
        }else {
            // 查到用户，在验证密码
            boolean flg = bCryptPasswordEncoder.matches(password,user.getPassword());
            // 密码错误
            if(!flg) {
                return new ResponseBodyMessage<>(-1,"用户名或者密码错误！",user);
            }
            // 验证账号密码成功，创建session并保存用户信息
            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
            return new ResponseBodyMessage<>(0,"登录成功！",user);
        }
    }


}
